{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": "'hdfs://localhost:9000/data/ten_million_top1k.csv'"
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 使用submitter对api进行访问\n",
    "from sparksampling import Submitter\n",
    "from sparksampling.var import FILE_TYPE_CSV\n",
    "from sparksampling.var import SIMPLE_RANDOM_SAMPLING_METHOD\n",
    "submitter = Submitter()\n",
    "dataset_uri = 'hdfs://localhost:9000/data/ten_million_top1k.csv'\n",
    "fraction = 0.1\n",
    "selected_features_list = ['X_20','X_80']\n",
    "label_index = 'y'\n",
    "dataset_uri"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2022-01-06 03:05:56,508 - INFO - request: http://localhost:8000/v1/sampling/simplejob/ with data {'path': 'hdfs://localhost:9000/data/ten_million_top1k.csv', 'method': 'random', 'type': 'csv', 'with_header': True, 'conf': {'fraction': 0.1}}\n"
     ]
    },
    {
     "data": {
      "text/plain": "{'code': 0, 'msg': '', 'data': {'job_id': 10019}}"
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 提交抽样任务\n",
    "submit_response = submitter.submit_sampling_simplejob(dataset_uri,\n",
    "                                              method=SIMPLE_RANDOM_SAMPLING_METHOD,\n",
    "                                              file_type=FILE_TYPE_CSV,\n",
    "                                              fraction=fraction,\n",
    "                                              with_header=True)\n",
    "job_id = submit_response.job_id\n",
    "submit_response.to_dict()"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2022-01-06 03:06:08,373 - INFO - request: http://localhost:8000/v1/query/sampling/job/ with data {'job_id': 10019}\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hdfs://localhost:9000/data/ten_million_top1k.csv-sampled-1641467156.5231845\n"
     ]
    }
   ],
   "source": [
    "# 查询抽样任务\n",
    "sampling_job_details = submitter.get_sampling_job_details(job_id)\n",
    "sampled_path = sampling_job_details.sampled_path\n",
    "# print(sampling_job_details.to_dict())\n",
    "# sampled_path\n",
    "print(sampled_path)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2022-01-06 03:07:19,480 - INFO - request: http://localhost:8000/v1/evaluation/job/ with data {'method': 'random-forest-spark', 'type': 'csv', 'compare_job_id': 10019, 'selected_features_list': ['X_20', 'X_80'], 'label': 'y', 'round': 3, 'maxDepth': 5}\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'code': 0, 'msg': '', 'data': {'job_id': 50023}}\n"
     ]
    },
    {
     "data": {
      "text/plain": "50023"
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\n",
    "rf_conf = {\n",
    "    \"compare_job_id\": job_id,\n",
    "    \"type\": \"csv\",\n",
    "    \"method\": \"random-forest-spark\",\n",
    "    \"selected_features_list\": selected_features_list,\n",
    "    \"label\": \"y\",\n",
    "    \"round\": 3,\n",
    "    \"maxDepth\":5\n",
    "}\n",
    "\n",
    "# 提交评估任务\n",
    "rf_evaluation_job = submitter.submit_evaluation_job(**rf_conf)\n",
    "print(rf_evaluation_job.to_dict())\n",
    "rf_evaluation_job_id = rf_evaluation_job.job_id\n",
    "rf_evaluation_job_id"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2022-01-06 03:07:51,990 - INFO - request: http://localhost:8000/v1/query/evaluation/job/ with data {'job_id': 50023}\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'score': 94, 'accuracy': 0.887}\n"
     ]
    }
   ],
   "source": [
    "cmp_evaluation_job_data = submitter.get_evaluation_job_details(job_id=rf_evaluation_job_id)\n",
    "print(cmp_evaluation_job_data.result)\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2022-01-06 03:07:55,966 - INFO - request: http://localhost:8000/v1/evaluation/job/ with data {'method': 'random-forest', 'type': 'csv', 'compare_job_id': 10019, 'key': 'y', 'selected_features_list': ['X_20', 'X_80']}\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'code': 0, 'msg': '', 'data': {'job_id': 50024}}\n"
     ]
    },
    {
     "data": {
      "text/plain": "50024"
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from sparksampling.var import EVALUATION_KMEANS_METHOD\n",
    "conf = {\n",
    "\"compare_job_id\": job_id,\n",
    "\"type\": \"csv\",\n",
    "\"method\": \"random-forest\",\n",
    "\"key\": \"y\",\n",
    "\"selected_features_list\": selected_features_list\n",
    "}\n",
    "# 提交评估任务\n",
    "cmp_evaluation_job = submitter.submit_evaluation_job(**conf)\n",
    "print(cmp_evaluation_job.to_dict())\n",
    "cmp_evaluation_job_id = cmp_evaluation_job.job_id\n",
    "cmp_evaluation_job_id"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2022-01-06 03:07:59,585 - INFO - request: http://localhost:8000/v1/query/evaluation/job/ with data {'job_id': 50024}\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'report': '              precision    recall  f1-score   support\\n\\n           0       0.87      0.87      0.87       517\\n           1       0.86      0.86      0.86       483\\n\\n    accuracy                           0.86      1000\\n   macro avg       0.86      0.86      0.86      1000\\nweighted avg       0.86      0.86      0.86      1000\\n', 'balanced_score': 0.8637745233489914, 'accuracy_score': 0.864, 'f1_score': 0.8638235152757974, 'precision_score': 0.8607068607068608}\n",
      "              precision    recall  f1-score   support\n",
      "\n",
      "           0       0.87      0.87      0.87       517\n",
      "           1       0.86      0.86      0.86       483\n",
      "\n",
      "    accuracy                           0.86      1000\n",
      "   macro avg       0.86      0.86      0.86      1000\n",
      "weighted avg       0.86      0.86      0.86      1000\n",
      "\n",
      "0.8637745233489914\n"
     ]
    }
   ],
   "source": [
    "cmp_evaluation_job_data = submitter.get_evaluation_job_details(job_id=cmp_evaluation_job_id)\n",
    "print(cmp_evaluation_job_data.result)\n",
    "report = cmp_evaluation_job_data.result['report']\n",
    "balanced_score = cmp_evaluation_job_data.result['balanced_score']\n",
    "print(report)\n",
    "print(balanced_score)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}